package bakudo.systems.finiteStateMachines {
	import bakudo.systems.finiteStateMachines.model.IState;	

	/**
	 * @author Micha� Rabi�ski
	 */
	public class FiniteStatesMachine {

		//::::::::::::::: private vars ::::::::::::::://

		// aktualny stan w, ktorym znajduje sie dany obiekt
		private var currentState : IState;

		// poprzedni wywolany stan
		private var previousState : IState;

		// nastepny wywolywany stan
		private var _nextState : IState;

		public function FiniteStatesMachine() {
			currentState = null;
			previousState = null;
			_nextState = null;
		}

		//----------------------------------------------------------------------
		// SETTERS
		//----------------------------------------------------------------------
		public function set nextState(state : IState) : void {
			_nextState = state;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// PUBLIC METHODS
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		public function update(time : Number) : void {
			if(currentState) {
				currentState.update(time);
			}
		}

		public function changeState(state : IState) : void {
			if(currentState)currentState.exit();
			previousState = currentState;
			currentState = state;
			currentState.enter();
		}

		public function gotoPreviousState() : void {
			changeState(previousState);
		}

		public function gotoNextState() : void {
			changeState(_nextState);
		}
	}
}
